package com.trg.generaltool.service.rabbitmq.route;

import com.rabbitmq.client.*;
import com.trg.generaltool.util.rabbitmq.RabbitmqUtil;

import java.io.IOException;

/**
 * @ClassName: RouteCustomer1
 * @Description: 发布/订阅者模式-消费者一
 * @Author: tangrg
 * @Date: 2021/7/19 11:44
 * @Version: 1.0
 * @Return:
 **/
public class RouteCustomer2 {


    public static void main(String[] args) throws IOException {
        //获取连接
        Connection connect = RabbitmqUtil.getConnect();
        //获取通道
        Channel channel = connect.createChannel();
        //通道和交换机绑定
        channel.exchangeDeclare("logs_direct", "direct");
        //获取一个临时的队列
        String queue = channel.queueDeclare().getQueue();
        //队列和交换机进行绑定
        channel.queueBind(queue, "logs_direct", "warning");

        //消费消息
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("当前消费信息：" + new String(body));
            }
        });


    }
}
